package com.pca.headline.controller;

import com.pca.headline.common.Result;
import com.pca.headline.common.ResultCodeEnum;
import com.pca.headline.pojo.NewsUser;
import com.pca.headline.service.NewsUserService;
import com.pca.headline.service.impl.NewsUserServiceImpl;
import com.pca.headline.util.JwtHelper;
import com.pca.headline.util.MD5Util;
import com.pca.headline.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/*")
public class NewsUerController extends BaseController{
    private NewsUserService userService = new NewsUserServiceImpl();
    /**
     * 登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取对象
        NewsUser paramUser = WebUtil.readJson(req, NewsUser.class);
        NewsUser loginUser =  userService.findByUsername(paramUser.getUsername());
        Result result = null;
        if(loginUser != null) {

            if(MD5Util.encrypt(paramUser.getUserPwd()).equalsIgnoreCase(loginUser.getUserPwd())) {
                Map data = new HashMap();
                data.put("token", JwtHelper.createToken(loginUser.getUid().longValue()));
                result = Result.ok(data);
            }else {
                result = Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
            }
        }else {
            result = Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        WebUtil.writeJson(resp,result);


    }



    protected void getUserInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String token = req.getHeader("token");
        Result result = Result.build(null, ResultCodeEnum.NOTLOGIN);
        if(token != null && (!token.equals(""))) {
            if(!JwtHelper.isExpiration(token)) {
                Integer userId = JwtHelper.getUserId(token).intValue();
                NewsUser newsuser = userService.findByUid(userId);
                newsuser.setUserPwd("");
                if(newsuser != null) {
                    Map data = new HashMap();
                    data.put("loginUser", newsuser);
                    result = Result.ok(data);
                }
            }
        }
        WebUtil.writeJson(resp,result);
    }


    protected void checkUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        Result result = Result.build(null, ResultCodeEnum.USERNAME_USED);
        NewsUser newsuser = userService.findByUsername(username);
        if(newsuser == null) {
            result = Result.ok(null);
        }
        WebUtil.writeJson(resp,result);
    }



    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsUser user = WebUtil.readJson(req, NewsUser.class);
        Result result = Result.ok(null);
        Integer rows= userService.registUser(user);
        if(rows == 0) {
            result = Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        WebUtil.writeJson(resp,result);
    }


    protected void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String token = req.getHeader("token");
        Result result = Result.build(null, ResultCodeEnum.NOTLOGIN);
        if(token != null && (!token.equals(""))) {
            if(!JwtHelper.isExpiration(token)) {
                result = Result.ok(null);
            }
        }
        WebUtil.writeJson(resp,result);
    }
}
